/**
 * Created by hps on 17-4-30.
 */
public class ParsePost {
    private StackX2 theStack;
    private String input;

    public ParsePost(String input) {
        this.input = input;
    }

    public int doParse() {
        theStack = new StackX2(20);
        char ch;
        int num1, num2, interAns;

        for(int j = 0, len = input.length(); j < len; j++) {
            ch = input.charAt(j);
            theStack.displayStack(" " + ch + " ");
            if(ch >= '0' && ch <= '9')
                theStack.push(Integer.parseInt(String.valueOf(ch)));
            else {
                num2 = theStack.pop();
                num1 = theStack.pop();
                switch (ch) {
                    case '+':
                        interAns = num1 + num2;
                        break;
                    case '-':
                        interAns = num1 - num2;
                        break;
                    case '*':
                        interAns = num1 * num2;
                        break;
                    case '/':
                        interAns = num1 / num2;
                        break;
                        default:
                            interAns = 0;
                }
                theStack.push(interAns);
            }
        }
        return theStack.pop();
    }
}
